﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VoteUphold
{
    /// <summary>
    ///  投票间隔秒数
    /// </summary>
    public class StopSecond
    {
        /// <summary>
        /// 投票间隔秒数 最小
        /// </summary>
        public int MinStopSecond { get; set; }

        /// <summary>
        /// 投票间隔秒数 最大
        /// </summary>
        public int MaxStopSecond { get; set; }

        /// <summary>
        /// 随机休息时间 计算
        /// </summary>
        /// <param name="secondDefalut">默认间隔</param>
        /// <param name="second">1.间隔秒数最小与最大的</param>
        /// <param name="separator">最小数，与最大数之间的间隔字符</param>
        /// <returns></returns>
        public static StopSecond GetStopSecond(string secondDefalut, string second, string separator)
        {
            StopSecond stopSecond = new StopSecond();
            try
            {
                if (string.IsNullOrEmpty(second))
                {
                    second = secondDefalut;
                }
                int indexof = second.IndexOf(separator);
                if (indexof > 0)
                {
                    int minStopSecond = Convert.ToInt32(second.Substring(0, indexof));
                    int maxStopSecond = Convert.ToInt32(second.Substring(indexof + 1));
                    if (minStopSecond > maxStopSecond)
                    {
                        minStopSecond = minStopSecond + maxStopSecond;
                        maxStopSecond = minStopSecond - maxStopSecond;
                        minStopSecond = minStopSecond - maxStopSecond;
                    }
                    stopSecond.MinStopSecond = minStopSecond;
                    stopSecond.MaxStopSecond = maxStopSecond;
                }
                else
                {
                    stopSecond.MaxStopSecond = Convert.ToInt32(second);
                }
            }
            catch (Exception)
            {
                stopSecond = GetStopSecond(secondDefalut,null, separator);
            }
            return stopSecond;
        }
    }
}
